<template>
  <div class="app-container">
    <!-- v-permission="['eqt:equipment:read']"
    v-permission="['eqt:equipment:create']"
     导入
    v-permission="['eqt:equipment:import']"
     生成打印
    v-permission="['eqt:equipment:read']"
      打印
    v-permission="['eqt:equipment:read']"
    v-permission="['eqt:equipment:update']"
    v-permission="['eqt:equipment:delete']" -->
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-select v-model="listQuery.fsiteNo" clearable filterable class="filter-item" placeholder="请选择所属学校" @change="getFpointOptions(listQuery.fsiteNo,1)">
        <el-option
          v-for="item in fsiteNoBelongOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-select v-model="listQuery.fpointNo" clearable filterable class="filter-item" placeholder="请选择监测点">
        <el-option
          v-for="item in fpointOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-input v-model.trim="listQuery.fequipmentbarcode" clearable class="filter-item" style="width: 200px;" placeholder="请输入位置码" type="number"/><!-- oninput="value=value.replace(/[^\d]/g,'')"-->
      <el-input v-model.trim="listQuery.fmacno" clearable class="filter-item" style="width: 200px;" placeholder="请输入机器码"/> <!-- oninput="value=value.replace(/[^\d]/g,'')"-->
      <el-button class="filter-item" :style="{background:buttonColor.lookup,color:buttonColor.color}" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button class="filter-item" :style="{background:buttonColor.add,color:buttonColor.color}" icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <el-upload
        :on-success="uploadFileSuccess"
        :headers="headers"
        :action="uploadPath"
        :show-file-list="false"
        class="filter-item"
        accept=".xlsx">
        <el-button icon="el-icon-upload" :style="{background:buttonColor.import,color:buttonColor.color}">导入</el-button>
      </el-upload>
      <el-button v-show="!printed" :disabled="isPrint" class="filter-item" :style="{background:buttonColor.addprint,color:buttonColor.color}" @click="printed=true">生成打印</el-button>
      <el-button v-print="'#printMer'" v-show="!isPrint && printed" class="filter-item" :style="{background:buttonColor.print,color:buttonColor.color}">打印</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" v-show="!printed" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column :index="indexMethod" align="center" label="序" type="index"/>
      <el-table-column :formatter="formatSite" align="center" label="所属学校" prop="fsiteNo"/>
      <!--<el-table-column align="center" label="监测点编号" prop="fpointNo"/>-->
      <el-table-column align="center" label="监测点名称" prop="fpointName"/>
      <el-table-column align="center" label="位置码" prop="fequipmentbarcode"/>
      <el-table-column align="center" label="设备名称" prop="fequipmentname"/>
      <el-table-column align="center" label="机器码" prop="fmacno"/>
      <el-table-column align="center" label="设备标签" prop="fequipmenttag"/>
      <el-table-column align="center" label="排插MAC" prop="flinkmac"/>
      <el-table-column align="center" label="标识符" prop="flinkmark"/>
      <!--<el-table-column align="center" label="是否连线" prop="flinkflag"/>
      <el-table-column align="center" label="状态" prop="fstate"/>-->
      <el-table-column align="center" width="140" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <el-button :style="{background:buttonColor.edit,color:buttonColor.color}" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button :style="{background:buttonColor.delete,color:buttonColor.color}" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 打印 -->
    <section id="printMer" ref="prints">
      <div v-show="printed" class="dashboard-editor-container" style="width: 700px">
        <el-row>
          <el-col align="center">
            <h3>{{ formatSite2(listQuery.fsiteNo) }}</h3>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4">
            <!--<div class="demo-image">-->
            <!-- <template>
              <qrcode :value="listQuery.fpointNo" :options="{ size: 100 }"/>
            </template> -->
            <!--</div>-->
          </el-col>
          <el-col :span="8">
            <div class="demo-image">
              <br><br><br>
              <h3>{{ formatPoint(listQuery.fpointNo) }}</h3>
            </div>
          </el-col>
        </el-row>
      </div>
      <el-table v-loading="listLoading" v-show="printed" :data="list2" element-loading-text="正在查询中。。。" border fit highlight-current-row style="width: 700px">

        <!--<el-table-column :index="indexMethod" align="center" label="序" type="index"/>-->
        <el-table-column align="center" label="设备名称" prop="fequipmentname"/>
        <el-table-column align="center" label="设备标签" prop="fequipmenttag"/>
        <el-table-column align="center" label="位置码" prop="fequipmentbarcode"/>
        <el-table-column label="二维码" prop="fequipmentbarcode">
          <!-- <template slot-scope="scope">
            <qrcode :value="scope.row.fequipmentbarcode" :options="{ size: 60 }"/>
          </template> -->
        </el-table-column>
        <el-table-column align="center" label="机器码" prop="fmacno"/>
        <el-table-column label="二维码" prop="fmacno">
          <!-- <template slot-scope="scope">
            <qrcode v-if="scope.row.fmacno" :value="scope.row.fmacno" :options="{ size: 60 }"/>
          </template> -->
        </el-table-column>
      </el-table>
    </section>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 500px; margin-left:20px;">
        <el-row>
          <el-col :span="10">
            <el-form-item label="所属学校" prop="fsiteNo" style="width: 250px">
              <el-select v-model="dataForm.fsiteNo" clearable filterable placeholder="请选择" @change="getFpointOptions(dataForm.fsiteNo,2)">
                <el-option
                  v-for="item in fsiteNoBelongOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="padding-left: 80px">
            <el-form-item label="监测点" prop="fpointNo" style="width: 250px">
              <el-select v-model="dataForm.fpointNo" clearable filterable class="filter-item" placeholder="请选择监测点">
                <el-option
                  v-for="item in fpointOptions2"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="位置码" prop="fequipmentbarcode" style="width: 250px">
              <el-input v-model="dataForm.fequipmentbarcode" :disabled="dialogStatus=='update'"/>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="padding-left: 80px">
            <el-form-item label="设备名称" prop="feuipmentid" style="width: 250px">
              <el-select v-model="dataForm.feuipmentid" clearable filterable placeholder="请选择">
                <el-option
                  v-for="item in sTsEquipmentOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="机器码" prop="fmacno" style="width: 250px">
              <el-input v-model="dataForm.fmacno"/>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="padding-left: 80px">
            <el-form-item label="设备标签" prop="fequipmenttag" style="width: 250px">
              <el-input v-model="dataForm.fequipmenttag"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="排插MAC" prop="flinkmac" style="width: 250px">
              <el-input v-model="dataForm.flinkmac"/>
            </el-form-item>
          </el-col>
          <el-col :span="10" style="padding-left: 80px">
            <el-form-item label="标识符" prop="flinkmark" style="width: 250px">
              <el-input v-model="dataForm.flinkmark"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :style="{background:buttonColor.cancel,color:buttonColor.colorone}" @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="createData">确定</el-button>
        <el-button v-else :style="{background:buttonColor.confirm,color:buttonColor.color}" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEquipment, createEquipment, updateEquipment, deleteEquipment, uploadPath } from '@/api/equipment'
import { getToken } from '@/utils/auth'
import { fsiteOptionsByLevel, sTsEquipmentOptions, fpointOptions } from '@/api/area'
import Pagination from '@/components/Pagination'
import moment from 'moment/moment'
import {buttonColor} from '@/filters/index'
// import QRcode from '@chenfengyuan/vue-qrcode'
export default {
  name: 'Equipment',
  components: { Pagination },
  data() {
    return {
      buttonColor:buttonColor,
      list: null,
      list2: null,
      total: 0,
      fsiteNoBelongOptions: null,
      fpointOptions: null,
      fpointOptions2: null,
      sTsEquipmentOptions: null,
      isPrint: true,
      printed: false,
      listLoading: true,
      image: 'https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top-e3b63a0b1b.png',
      uploadPath,
      listQuery: {
        page: 1,
        limit: 20,
        fpointNo: undefined,
        feuipmentid: undefined,
        fequipmentname: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      /*
      <el-table-column align="center" label="状态" prop="fstate"/>*/
      dataForm: {
        id: undefined,
        fequipmentbarcode: undefined,
        feuipmentid: undefined,
        fequipmentname: undefined,
        fmacno: undefined,
        fsiteNo: undefined,
        fpointNo: undefined,
        fpointName: undefined,
        flinkmac: undefined,
        flinkmark: undefined,
        frecbillno: undefined,
        frecdate: undefined,
        fprocdate: undefined,
        flosdate: undefined,
        flinkflag: undefined,
        fstate: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        fequipmentbarcode: [
          { required: true, message: '位置码不能为空', trigger: 'blur' }
        ],
        feuipmentid: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ],
        // fmacno: [
        //   { required: true, message: '机器码不能为空', trigger: 'blur' }
        // ],
        fequipmenttag: [
          { required: true, message: '设备标签不能为空', trigger: 'blur' }
        ],
        fsiteNo: [
          { required: true, message: '所属学校不能为空', trigger: 'blur' }
        ],
        fpointNo: [
          { required: true, message: '监测点不能为空', trigger: 'blur' }
        ]
        // flinkmac: [
        //   { required: true, message: '排插MAC不能为空', trigger: 'blur' }
        // ],
        // flinkmark: [
        //   { required: true, message: '标识符不能为空', trigger: 'blur' }
        // ]
      },
      permissionDialogFormVisible: false,
      systemPermissions: null,
      assignedPermissions: null,
      permissionForm: {
        roleId: undefined,
        permIds: []
      }
    }
  },
  computed: {
    headers() {
      return {
        'Authorization': getToken()
      }
    }
  },
  created() {
    this.getList()
    fsiteOptionsByLevel(3)
      .then(response => {
        this.fsiteNoBelongOptions = response.data.data.list
      })
      .catch(() => {
        this.fsiteNoBelongOptions = []
      })
    sTsEquipmentOptions()
      .then(response => {
        this.sTsEquipmentOptions = response.data.data.list
      })
      .catch(() => {
        this.sTsEquipmentOptions = []
      })
  },
  methods: {
    formatStatus: function(row, column) {
      return row.fsiteState === 1 ? '启用' : '禁用'
    },
    dateFormat(row, column) {
      const date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    formatSite: function(row, column) {
      for (let i = 0; i < this.fsiteNoBelongOptions.length; i++) {
        if (row.fsiteNo === this.fsiteNoBelongOptions[i].value) {
          return this.fsiteNoBelongOptions[i].label
        }
      }
      return ''
    },
    formatSite2: function(number) {
      for (let i = 0; i < this.fsiteNoBelongOptions.length; i++) {
        if (number === this.fsiteNoBelongOptions[i].value) {
          return this.fsiteNoBelongOptions[i].label
        }
      }
      return ''
    },
    formatPoint: function(number) {
      for (let i = 0; i < this.list.length; i++) {
        if (number === this.list[i].fpointNo) {
          return this.list[i].fpointName
        }
      }
      return ''
    },
    getList() {
      this.listLoading = true
      listEquipment(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
          this.isPrint = true
          this.printed = false
          if (this.listQuery.fpointNo != null) {
            this.isPrint = false
            this.list2 = response.data.data.list
          }
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // printout() {
    //   this.$print(this.$refs.print, { 'no-print': '.do-not-print-me-xxx' }) // 使用
    // },
    resetForm() {
      this.dataForm = {
        id: undefined,
        fequipmentbarcode: undefined,
        feuipmentid: undefined,
        fequipmentname: undefined,
        fmacno: undefined,
        fsiteNo: undefined,
        fpointNo: undefined,
        fpointName: undefined,
        flinkmac: undefined,
        flinkmark: undefined,
        frecbillno: undefined,
        frecdate: undefined,
        fprocdate: undefined,
        flosdate: undefined,
        flinkflag: undefined,
        fstate: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.dataForm.fequipmenttag = '1#'
      this.fpointOptions2 = []
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          createEquipment(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加教室设备成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      fpointOptions(row.fsiteNo)
        .then(response => {
          this.fpointOptions2 = response.data.data.list
        })
        .catch(() => {
          this.fpointOptions2 = []
        })
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updateEquipment(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.fequipmentbarcode === this.dataForm.fequipmentbarcode) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新教室设备资料成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除该教室设备资料', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteEquipment(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除教室设备资料成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response
            })
          })
      })
    },
    indexMethod(index) {
      index = index + 1
      return index
    },
    uploadFileSuccess: function(response) {
      this.$notify.success({
        title: '导入',
        message: response.data
      })
    },
    getFpointOptions(fsiteNo, inx) {
      this.listQuery.fpointNo = undefined
      fpointOptions(fsiteNo)
        .then(response => {
          if (inx === 1) {
            this.fpointOptions = response.data.data.list
          } else {
            this.fpointOptions2 = response.data.data.list
          }
        })
        .catch(() => {
          if (inx === 1) {
            this.fpointOptions = []
          } else {
            this.fpointOptions2 = []
          }
        })
    }
  }
}
</script>
<style>
  /*处理input type = number的上下箭头*/
  input::-webkit-outer-spin-button,
  input::-webkit-inner-spin-button {
    -webkit-appearance: none;
  }
  input[type="number"]{
    -moz-appearance: textfield;
  }
</style>
